package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.CheckGroupDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.pojo.CheckGroup;
import com.itheima.service.CheckGroupService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 检查组服务
 * @author qzh
 * @create 2020-01-07 2020/1/7
 */
@Service(interfaceClass = CheckGroupService.class)
@Transactional
public class CheckGroupServiceImpl implements CheckGroupService {

    @Autowired
    private CheckGroupDao checkGroupDao;


    //新增检查组
    @Override
    public void add(CheckGroup checkGroup, Integer[] checkitemIds) {

        //新增检查组
        checkGroupDao.add(checkGroup);
        //新增多对多的关联关系
        Integer checkGroupId = checkGroup.getId();
        if(checkitemIds!=null&&checkitemIds.length>0) {

            for (Integer checkitemId : checkitemIds) {
                checkGroupDao.setCheckGroupAndCheckItem(checkGroupId, checkitemId);
            }
        }

    }

    /**
     * 分页查询
     * @param queryPageBean
     * @return
     */
    @Override
    public PageResult pageQuery(QueryPageBean queryPageBean) {

        Integer currentPage = queryPageBean.getCurrentPage();

        Integer pageSize = queryPageBean.getPageSize();

        String queryString = queryPageBean.getQueryString();

        PageHelper.startPage(currentPage,pageSize);
        //Page<CheckGroup> page = checkGroupDao.findByCondition(queryPageBean);
        Page<CheckGroup> page = checkGroupDao.findByCondition(queryString);
        List<CheckGroup> result = page.getResult();
        long total = page.getTotal();
        PageResult pageResult = new PageResult(total,result);
        return pageResult;
    }

    @Override
    public CheckGroup findById(Integer id) {
        CheckGroup checkGroup = checkGroupDao.findById(id);
        return checkGroup;
    }

    @Override
    public List<Integer> findCheckItemIdsByCheckGroupId(Integer id) {

        return checkGroupDao.findCheckItemIdsByCheckGroupId(id);



    }

    @Override
    public void edit(CheckGroup checkGroup, Integer[] checkitemIds) {

        //修改检查组
        checkGroupDao.edit(checkGroup);


        //删除所有关系
        checkGroupDao.deleteCheckGroupAndCheckItemRel(checkGroup.getId());

        //重新建立关系
        if(checkitemIds!=null&&checkitemIds.length>0){
            for (Integer checkitemId : checkitemIds) {
                checkGroupDao.addCheckGroupAndCheckItemRel(checkitemId,checkGroup.getId());
            }
        }


    }

    @Override
    public List<CheckGroup> findAll() {

        return checkGroupDao.findAll();

    }

    @Override
    public void deleteById(Integer id) {

        //先查询中间表  是否有checkgroupid  对应的行
        long count = checkGroupDao.findCountByCheckGroupId(id);
        if(count>0){
            checkGroupDao.deleteCheckGroupAndCheckItemRel(id);
        }
        checkGroupDao.deleteCheckGroupById(id);


    }
}
